<template>
    <div class="app-container">
        <div class="filter-container">
            <el-input v-model="listQuery.name" placeholder="昵称" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter" />
            <el-input v-model="listQuery.name" placeholder="所属账号" style="width: 200px;" class="filter-item"
            @keyup.enter.native="handleFilter" />
            <!-- <el-select v-model="listQuery.importance" placeholder="分组" clearable style="width: 90px" class="filter-item">
                <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
            </el-select> -->
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                查询
            </el-button>
            <!-- <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                @click="handleCreate">
                导入好友
            </el-button> -->
        </div>
        <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
            <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80"
                :class-name="getSortClass('id')">
                <template slot-scope="{row}">
                    <span>{{ row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column label="昵称" width="150px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.nc }}</span>
                </template>
            </el-table-column>
            <el-table-column label="好友账号" width="150px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.hyphone }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属账号" width="150px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.sszh }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="所属账号" width="120px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.tjtype==1?'主动':'被动' }}</span>
                </template>
            </el-table-column> -->
            <el-table-column label="聊天记录" width="150px" align="center">
                <template slot-scope="{row}">
                    <el-button @click="getlt(row)" type="primary" plain>查看记录</el-button>
                    <!-- <span>{{ row.tjtype==1?'主动':'被动' }}</span> -->
                </template>
            </el-table-column>
            <el-table-column label="创建时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <el-tag>{{ row.times | parseTime('{y}-{m}-{d} {h}:{i}') }} </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
                <template slot-scope="{row,$index}">
                    <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
            @pagination="getList" />
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px"
                style="width: 400px; margin-left:50px;">
                <el-form-item label="所属账号" label-width="100px">
                   
                </el-form-item>
                <el-form-item label="好友" label-width="100px">
                    
                </el-form-item>
                
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">
                    取消
                </el-button>
                <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
                    导入好友
                </el-button>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
            <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
                <el-table-column prop="key" label="Channel" />
                <el-table-column prop="pv" label="Pv" />
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchList, fetchPv, createArticle, updateArticle, getDel, jcList, Addrw, getListAll,getImplode } from '@/api/Hylist'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import UploadExcelComponent from '@/components/UploadExcel/maxindex.vue'
const statusOptions = [
    { key: 1, display_name: '正常' },
    { key: 2, display_name: '暂停' },
    { key: 3, display_name: '封禁' },
    { key: 4, display_name: '异常' }
]
export default {
    name: 'ComplexTable',
    components: { Pagination, UploadExcelComponent },
    directives: { waves },
    filters: {
        statusFilter(status) {
            const statusMap = {
                1: 'success',
                2: 'info',
                3: 'danger'
            }
            return statusMap[status]
        },
        typeFilter(type) {
            return calendarTypeKeyValue[type]
        }
    },
    data() {
        return {
            tableKey: 0,
            list: null,
            listAll: null,
            total: 0,
            listLoading: true,
            listQuery: {
                page: 1,
                limit: 20,
                types: 1,
                name: '',
            },
            statusOptions,
            jcOptions: [],
            multipleSelection: [],
            showReviewer: false,
            temp: {
                id: undefined,
                phonelist: '',
                bid: ''
            },
            dialogFormVisible: false,
            dialogStatus: '',
            textMap: {
                update: 'Edit',
                create: '检测号码'
            },
            dialogPvVisible: false,
            pvData: [],
            rules: {
                type: [{ required: true, message: 'type is required', trigger: 'change' }],
                title: [{ required: true, message: 'title is required', trigger: 'blur' }]
            },
            downloadLoading: false
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getlt(row){
            this.$notify({
                title: 'Success',
                message: '暂无接口 等待数据',
                type: 'success',
                duration: 2000
            })
        },
        alltypes() {
            let list = this.multipleSelection
            let arr = []
            list.forEach(function (item, index) {
                console.log(item); //输出数组的每一个元素
                if (item.logonstatus != 2) {
                    arr.push(item.id)
                }
            });
            if (arr.length > 0) {
                //进行登陆操作
                let temp = {
                    ids: arr.join(",")
                }
                this.listLoading = true
                Addrw(temp).then(response => {
                    this.$notify({
                        title: 'Success',
                        message: 'Created Successfully',
                        type: 'success',
                        duration: 2000
                    })
                    this.getList();
                })
            }
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log('导入数据', this.multipleSelection)
        },
        zx(row, index) {
            let arr = []
            arr.push(row.id)
            let data = {
                ids: arr.join(",")
            }
            Addrw(data).then(response => {
                this.$notify({
                    title: 'Success',
                    message: 'Created Successfully',
                    type: 'success',
                    duration: 2000
                })
                this.getList();
            })
        },
        getJcList() {
            jcList().then(response => {
                console.log(response.data)
                this.jcOptions = response.data.list
            })
        },
        beforeUpload(file) {
            const isLt1M = file.size / 1024 / 1024 < 1
            if (isLt1M) {
                return true
            }
            this.$message({
                message: 'Please do not upload files larger than 1m in size.',
                type: 'warning'
            })
            return false
        },
        handleSuccess({ results, header }) {
            console.log(results)
            console.log(header)
            let listall = {
                dataall:results
            }
            getImplode(listall).then(response => {
                this.dialogFormVisible = false
                this.getList()
            })

        },
        getListAlls() {
            getListAll({ status: 2 }).then(response => {
                this.listAll = response.data.list
            })
        },
        getList() {
            this.listLoading = true
            fetchList(this.listQuery).then(response => {
                console.log(response.data)
                this.list = response.data.list
                this.total = response.data.count
                setTimeout(() => {
                    this.listLoading = false
                }, 1.5 * 1000)
            })
        },
        handleFilter() {
            this.listQuery.page = 1
            this.getList()
        },
        handleModifyStatus(row, status) {
            this.$message({
                message: '操作Success',
                type: 'success'
            })
            row.status = status
        },
        sortChange(data) {
            const { prop, order } = data
            if (prop === 'id') {
                this.sortByID(order)
            }
        },
        sortByID(order) {
            if (order === 'ascending') {
                this.listQuery.sort = '+id'
            } else {
                this.listQuery.sort = '-id'
            }
            this.handleFilter()
        },
        resetTemp() {
            this.temp = {
                id: undefined,
                name: '',
                status: 1,
            }
        },
        handleCreate() {
            this.dialogStatus = 'create'
            this.dialogFormVisible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].clearValidate()
            })
        },
        createData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    createArticle(this.temp).then(() => {
                        this.dialogFormVisible = false
                        this.$notify({
                            title: 'Success',
                            message: 'Created Successfully',
                            type: 'success',
                            duration: 2000
                        })
                        this.getList();
                    })
                }
            })
        },
        handleUpdate(row) {
            this.temp = Object.assign({}, row) // copy obj
            this.dialogStatus = 'update'
            this.dialogFormVisible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].clearValidate()
            })
        },
        updateData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const tempData = Object.assign({}, this.temp)
                    updateArticle(tempData).then(() => {
                        const index = this.list.findIndex(v => v.id === this.temp.id)
                        this.list.splice(index, 1, this.temp)
                        this.dialogFormVisible = false
                        this.$notify({
                            title: 'Success',
                            message: 'Update Successfully',
                            type: 'success',
                            duration: 2000
                        })
                    })
                }
            })
        },
        handleDelete(row, index) {
            let temp = {
                id: row.id
            }
            getDel(temp).then(() => {
                this.$notify({
                    title: 'Success',
                    message: 'Created Successfully',
                    type: 'success',
                    duration: 2000
                })
            })
            this.list.splice(index, 1)
        },
        handleFetchPv(pv) {
            fetchPv(pv).then(response => {
                this.pvData = response.data.pvData
                this.dialogPvVisible = true
            })
        },
        handleDownload() {
            this.downloadLoading = true
            import('@/vendor/Export2Excel').then(excel => {
                const tHeader = ['号码', '密钥']
                const filterVal = ['phone', 'tokens']
                const data = this.formatJson(filterVal)
                excel.export_json_to_excel({
                    header: tHeader,
                    data,
                    filename: '已注册号码'
                })
                this.downloadLoading = false
            })
        },
        formatJson(filterVal) {
            return this.listAll.map(v => filterVal.map(j => {
                return v[j]
            }))
        },



        getSortClass: function (key) {
            const sort = this.listQuery.sort
            return sort === `+${key}` ? 'ascending' : 'descending'
        }
    }
}
</script>
